/* eslint-disable keyword-spacing,indent */
// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------

module.exports = class extends think.cmswing.admin {
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
    this.tactive = 'member';
  }
  /**
   * index action
   * @return {Promise} []
   */
  async indexAction() {
    const status = this.get('status');
    const map = {};
    if (!think.isEmpty(status)) {
      map.status = status;
      this.assign('status', status);
    }
    const q = this.get('q');
    if (!think.isEmpty(q)) {
      map['real_name|usermobile|usergsmc|xinxibiaoti'] = ['like', '%' + q + '%'];
    }
    const data = await this.model('consult').where(map).page(this.get('page') || 1, 20).order('id desc').countSelect();
    const html = this.pagination(data);
    this.assign('pagerData', html); // 分页展示使用
    // console.log(data.data);
    this.active = 'admin/consult/index';
    this.assign('list', data.data);
    this.meta_title = '咨询管理';
    return this.display();
  }

  /**
     * 备注咨询
     */
    async remarkAction() {
      if (this.isPost) {
        const id = this.post('id');
        const adminRemark = this.post('admin_remark');
        const remark = await this.model('consult').where({id: id}).update({remark: adminRemark});
        if (remark) {
          return this.success({name: '操作成功！', url: this.referer()});
        } else {
          return this.fail('操作失败！');
        }
      } else {
        const id = this.get('id');
        this.assign('id', id);
        this.meta_title = '备注咨询';
        return this.display();
      }
    }
    async seeAction() {
      const id = this.get('id');
      this.meta_title = '查看咨询';
      // 获取咨询信息
      const consult = await this.model('consult').find(id);
      // 购买人信息
      const member = await this.model('member').find(consult.uid);
      const gongxu = await this.model('gongxufabu').find(consult.gongxuid);
      this.assign('user', member);
      this.assign('gongxufabu', gongxu);
      this.assign('consult', consult);
      return this.display();
    }
};
